import 'package:flutter/services.dart';

part 'windows.dart';

const MethodChannel _channel = MethodChannel('system.windows');

class SystemWindows {
  factory SystemWindows() => _singleton ??= SystemWindows._();

  SystemWindows._();

  static SystemWindows? _singleton;

  Future<bool> show(WindowsBox windows) async {
    Map<String, dynamic> map = {};
    if (windows is TextBox) {
      assert(windows.num > 0 && windows.num < 4);
      map = windows.toMap();
    } else if (windows is RecordBox) {
      map = windows.toMap();
    }
    final bool? state = await _channel.invokeMethod('show', map);
    return state ?? false;
  }

  /// 弹窗是否显示
  Future<bool> isActive() async =>
      (await _channel.invokeMethod<bool>('isActive')) ?? false;

  /// 是否支持
  Future<bool> get isSupported async {
    final bool? state = await _channel.invokeMethod('isSupported');
    return state ?? false;
  }

  /// 关闭所有并销毁
  Future<bool> dispose() async {
    final bool? state = await _channel.invokeMethod('dispose');
    return state ?? false;
  }

  /// 切换前后台
  Future<void> toggle(AppStatus state) =>
      _channel.invokeMethod('toggle', state == AppStatus.foreground);
}

enum AppStatus {
  /// 前台
  foreground,

  /// 后台
  background,
}
